/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cageballserver.scoreboard;

import cageballcommon.enummerations.TeamEnum;
import cageballcommon.messages.ScoreBoard;
import cageballserver.CageballClient;
import cageballserver.Game;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author kanonas
 */
public class ScoreBoardTournament {

    private Map<CageballClient, ScoreBoardEntry> scoreBoard;
    private List<Game> games;

    public ScoreBoardTournament(Collection<CageballClient> clients) {

        scoreBoard = new HashMap<CageballClient, ScoreBoardEntry>();
        games = new ArrayList<Game>();

        for (CageballClient cageballClient : clients) {
            scoreBoard.put(cageballClient, new ScoreBoardEntry(cageballClient));
        }
    }

    public void update(Game game){
        games.add(game);

        ScoreBoardEntry team1 = scoreBoard.get(game.getTeam1());
        ScoreBoardEntry team2 = scoreBoard.get(game.getTeam2());

        team1.update(TeamEnum.myTeam, game);
        team2.update(TeamEnum.otherTeam, game);
    }

    public List<ScoreBoard> getScoreBoard(){

        List<ScoreBoardEntry> list = new ArrayList<ScoreBoardEntry>(scoreBoard.values());
        Collections.sort(list);
        
        List<ScoreBoard> scores = new ArrayList<ScoreBoard>();

        for (ScoreBoardEntry e : list) {
            scores.add(new ScoreBoard(e.getTeamName(), e.getPoints(), e.getScores(), e.getConcedes(), e.getWins(), e.getDraws(), e.getDefeats()));
        }

        return scores;
    }
}
